package com.ad.backstage.controller;

import com.ad.backstage.service.CategoryService;
import com.ad.payload.dto.trade.ControllerNameDto;
import com.ad.payload.dto.trade.QuerySubCategoryDto;
import com.ad.payload.dto.trade.UpdateCategoryByMerchantIdDto;
import com.ad.payload.model.IdDto;
import com.ad.payload.util.StringUtil;
import com.ad.payload.vo.trade.ParentCategoryVo;
import com.ad.payload.vo.trade.SubCategoryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

import static com.ad.backstage.service.impl.CategoryServiceImpl.FIRST_CATEGORY_MAP;

/**
 * @author:dxgong
 * @日期 2021/6/15 15:56
 * @注释
 */
@RestController
@Slf4j
@RequestMapping("CategoryController")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    /**
     * 查询一级行业
     * */
    @RequestMapping("/back/auth/queryFirstCategory")
    public List<String> queryFirstCategory(){
        List<ParentCategoryVo> parentCategoryVos = categoryService.queryFirstCategory();
        List<String> list = new ArrayList<>();
        parentCategoryVos.forEach(item -> {
            list.add(item.getName());
        });
        return list;
    }

    /**
     * 根据trade_id询二级行业
     *
     * @param querySubCategoryDto
     * */
    @RequestMapping("/back/auth/querySecondCategory")
    public List<String> querySecondCategory(@Valid @RequestBody QuerySubCategoryDto querySubCategoryDto){
        Long id = FIRST_CATEGORY_MAP.get(querySubCategoryDto.getParentCategoryName());
        IdDto idDto = new IdDto();
        idDto.setId(id);
        List<String> list = new ArrayList<>();
        List<SubCategoryVo> subCategoryVos = categoryService.querySecondCategory(idDto);
        subCategoryVos.forEach(item -> {
            list.add(item.getName());
        });
        return list;
    }


    /**
     * 根据行业名称查询id
     * */
    @RequestMapping("/back/auth/queryCategoryIdByName")
    public List<ParentCategoryVo> queryCategoryIdByName(@Valid @RequestBody ControllerNameDto controllerNameDto){
        // 查询一级行业
        ParentCategoryVo parentCategoryVo = categoryService.queryFirstCategoryByName(controllerNameDto.getFirstName());

        List<ParentCategoryVo> rtnList = new ArrayList<>();

        rtnList.add(parentCategoryVo);

        // 查询二级行业
        if(StringUtil.isNotBlank(controllerNameDto.getSecondName())){
            SubCategoryVo subCategoryVo = categoryService.querySecondCategoryByName(controllerNameDto.getSecondName());
            ParentCategoryVo vo = new ParentCategoryVo();
            vo.setId(subCategoryVo.getId());
            vo.setName(subCategoryVo.getName());
            rtnList.add(vo);
        }

        // 返回 0下标是一级行业，1下标是二级行业
        return rtnList;
    }


    @PostMapping("/back/noAuth/queryCategoryIdByName")
    public void updateCategoryByMerchantId(@Valid @RequestBody UpdateCategoryByMerchantIdDto dto){
        log.info("========= dto:" + dto);
        categoryService.updateCategoryByMerchantId(dto);
    }

}
